package multiDemo;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;

public class ReceiveDemo1 {
    public static void main(String[] args) throws IOException {
        //接受数据端
        //1.创建对象
        MulticastSocket ms = new MulticastSocket(10000);
        //2.添加本机ip
        InetAddress address = InetAddress.getByName("224.0.0.1");
        ms.joinGroup(address);
        //3.创建接受端数据包对象
        byte[] bytes = new byte[1024];
        DatagramPacket dp = new DatagramPacket(bytes, bytes.length);
        //4.接收数据并解析
        ms.receive(dp);
        //5.打印数据

        byte[] data = dp.getData();
        int len = dp.getLength();
        String ip = dp.getAddress().getHostAddress();
        String name = dp.getAddress().getHostName();

        System.out.println("ip:" + ip + "数据: " + (new String(data, 0, len)));
        ms.close();
    }
}
